import cv2
import numpy as np

def ellipse_detect_rgb(image):
    """
    :param image: 图片路径
    :return: None
    """
    img = cv2.imread(image)
    YCRCB = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    (y, cr, cb) = cv2.split(YCRCB)
    skinCrCbHist = np.zeros((cr.shape), dtype=np.uint8)
    cv2.ellipse(skinCrCbHist, (113, 155), (23, 15), 43, 0, 360, (255, 255, 255), -1)
    skin = np.zeros(cr.shape, dtype=np.uint8)
    (x, y) = cr.shape
    for i in range(0, x):
        for j in range(0, y):
            CR = YCRCB[i, j, 1]
            CB = YCRCB[i, j, 2]
            if skinCrCbHist[CR, CB] > 0:
                skin[i, j] = 255
    cv2.namedWindow(image, cv2.WINDOW_NORMAL)
    cv2.imshow(image, img)
    dst = cv2.bitwise_and(img, img, mask=skin)
    cv2.namedWindow("rgb", cv2.WINDOW_NORMAL)
    cv2.imshow("rgb", dst)
    cv2.waitKey()


def ellipse_detect_hsv(image):
    """
    :param image: 图片路径
    :return: None
    """
    # 读取图像，声明存储图像的变量
    img = cv2.imread(image, cv2.IMREAD_COLOR)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    (_h, _s, _v) = cv2.split(hsv)
    skin = np.zeros(_h.shape, dtype=np.uint8)
    (x, y) = _h.shape
    # 0, 16, 0, 255, 153, 255
    for i in range(0, x):
        for j in range(0, y):
            if (_h[i][j] > 0) and (_h[i][j] < 16) and (_s[i][j] > 0) and (_s[i][j] < 255) and (_v[i][j] > 153) and (
                    _v[i][j] < 255):
                skin[i][j] = 255
            else:
                skin[i][j] = 0
    cv2.namedWindow(image, cv2.WINDOW_NORMAL)
    # 显示图像(window窗体的名字，详实的图像)
    cv2.imshow(image, img)
    dst = cv2.bitwise_and(img, img, mask=skin)
    cv2.namedWindow("hsv", cv2.WINDOW_NORMAL)
    cv2.imshow("hsv", dst)


if __name__ == '__main__':
    # ellipse_detect_hsv('testjms.png')
    ellipse_detect_rgb('people/1.png')